<template>
  <div>
    <!-- 房间列表 -->
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">房间配置管理</div>
        <!-- <div class="op_right">
          <button class="primary_button" @click="AddRoom">添加房间</button>
        </div> -->
      </div>

      <div class="table_list">
        <el-table :data="RoomConfigList" stripe style="width: 100%">
          <el-table-column prop="configName" label="配置名称" width="300">
          </el-table-column>
          <el-table-column prop="imageUrl" label="图片地址">
            <template slot-scope="scope">
              <img :src="scope.row.imageUrl" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="reorder" label="排序" width="300"> </el-table-column>
          <!-- <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="Edit(scope.row)"
                >编辑</el-button
              >
              <el-button size="mini" type="text" @click="DelSort(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column> -->
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 添加房间 -->
      <el-dialog
        :title="title"
        :visible.sync="newRoomdialogVisible"
        width="400px"
        :before-close="CancelRoomSubmit"
        class="no-dialog-padding"
      >
        <span>
          <div class="add_room">
            <el-form
              ref="newRoomForm"
              :rules="newRoomRules"
              :model="newRoomForm"
            >
              <el-row :gutter="40">
                <el-col :span="24">
                  <el-form-item label="排序号" prop="reorder">
                    <el-input-number
                      class="w-100 text-left"
                      placeholder="请输入排序号"
                      v-model="newRoomForm.reorder"
                      :min="0"
                      :step="1"
                      :precision="0"
                      :controls="false"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间名称" prop="name">
                    <el-input
                      class="w-100"
                      v-model="newRoomForm.name"
                      placeholder="请输入房间名称"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间楼层" prop="floorId">
                    <el-select
                      v-model="newRoomForm.floorId"
                      placeholder="请选择房间楼层"
                      class="w-100"
                    >
                      <el-option
                        v-for="item in floorList"
                        :key="item.id"
                        :label="item.value"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间分类" prop="typeId">
                    <el-select
                      v-model="newRoomForm.typeId"
                      placeholder="请选择"
                      class="w-100"
                    >
                      <el-option
                        v-for="item in sortList"
                        :key="item.id"
                        :label="item.value"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间床位" prop="bedsId">
                    <el-select
                      v-model="newRoomForm.bedsId"
                      placeholder="请选择房间床位"
                      class="w-100"
                    >
                      <el-option
                        v-for="item in bedList"
                        :key="item.id"
                        :label="item.value"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="CancelRoomSubmit"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="RoomSubmit"
            :loading="newRoomLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
    
    <script>
import {
  CreateRoom,
  CreateRoomconfigPage,
  UpdateRoomconfigPage,
  getRoomconfig,
  UpdateRoom,
  DeleteRoom,
  getRoomconfigPage,
} from "@/api/business/room";
export default {
  name: "EryuoaStoreRoomdisposition",

  data() {
    return {
      newRoomdialogVisible: false, // 新增房间
      newRoomLoading: false,
      loading: false,
      title: "",
      RoomConfigList: [], // 房间列表数据
      floorList: [], // 楼层列表
      sortList: [], // 楼层列表
      bedList: [], // 楼层列表

      RoomStateOptions: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "关闭",
        },
        {
          value: "1",
          label: "开启",
        },
      ],
      RoomData: [], // 客户来源数据

      // 房间状态
      RoomStateObj: {
        0: "关闭",
        1: "开启",
      },

      typeObj: {
        1: "充值",
        2: "消费",
        3: "钱包",
        4: "调整",
      },

      newRoomRules: {
        reorder: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        name: [{ required: true, message: "请输入房间名称", trigger: "blur" }],
        floorId: [
          { required: true, message: "请选择房间楼层", trigger: "blur" },
        ],

        typeId: [
          { required: true, message: "请选择房间分类", trigger: "blur" },
        ],
        bedsId: [
          { required: true, message: "请选择房间床位", trigger: "blur" },
        ],
      },

      newRoomForm: {
        reorder: "", // 排序
        name: "",
        bedsId: "",
        floorId: "",
        typeId: "", // 房间类型id
        typeName: "",
      },

      value: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        imageUrl: "",
      },

      // 性别
      Sexoptions: [
        {
          value: 0,
          label: "未知",
        },
        {
          value: 1,
          label: "男",
        },
        {
          value: 2,
          label: "女",
        },
      ],
      total: 0,
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    // 添加房间
    AddRoom() {
      this.newRoomdialogVisible = true;
      this.title = "添加房间";
    },

    // 表单重置
    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },

    // 新增房间 弹框 确认
    RoomSubmit() {
      this.$refs["newRoomForm"].validate((valid) => {
        if (valid) {
          if (!this.newRoomForm.roomId) {
            this.NewRoom();
          } else {
            this.EditRoom();
          }
        } else {
        }
      });
    },

    // 新增房间
    NewRoom() {
      CreateRoom(this.newRoomForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.CancelRoomSubmit();
          }
        })
        .catch((err) => {});
    },

    // 编辑房间
    EditRoom() {
      const { name, reorder, floorId, bedsId, typeId, typeName, roomId } =
        this.newRoomForm;
      UpdateRoom({
        name: name,
        reorder: reorder,
        floorId: floorId,
        bedsId: bedsId,
        typeId: typeId,
        typeName: typeName,
        roomId: roomId,
      })
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.CancelRoomSubmit();
          }
        })
        .catch((err) => {});
    },

    // 编辑房间
    Edit(row) {
      this.newRoomdialogVisible = true;
      this.title = "编辑房间";
      this.$nextTick(() => {
        let Item = JSON.parse(JSON.stringify(row));
        this.newRoomForm.name = Item.name;
        this.newRoomForm.reorder = Item.reorder;
        this.newRoomForm.floorId = Item.floorId;
        this.newRoomForm.bedsId = Item.bedsId;
        this.newRoomForm.typeId = Item.typeId;
        this.newRoomForm.roomId = Item.roomId;
      });
    },

    // 取消 弹框 关闭
    CancelRoomSubmit() {
      this.newRoomdialogVisible = false;
      this.newRoomForm.roomId = "";
      this.resetForm("newRoomForm");
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 房间列表
    getList() {
      this.loading = true;
      getRoomconfigPage(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.RoomConfigList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 商品列表 删除
    DelSort(row) {
      this.DeleteRoomList(row);
    },

    DeleteRoomList(row) {
      this.$confirm("房间删除后无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteRoom({ id: row.roomId })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              }
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.getList();
    },

    CancelRoomSubmit() {
      this.resetFormData("newRoomForm");
      this.$nextTick(() => {
        this.newRoomdialogVisible = false;
        this.newRoomLoading = false;
      });
    },
  },
};
</script>
    
    <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
  margin-left: 15px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}

.add_room {
  margin: 0 40px;
}
</style>